package com.example.demo.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Description:  普通的一个拦截器
 * User: 故我
 * Date: 2023-11-05
 * Time: 14:31
 */
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 返回值为 true 可以进行正常login  false 则被拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false); //没有找到不创建session
        if (session != null && session.getAttribute("username") != null) {
            return true;
        }
        //需要进行重新登录
        response.sendRedirect("/login.html");
        return false;
    }
}
